package t4tools.lepath.query.eval;

import t4tools.lepath.generate.PropertyGetter;
import t4tools.lepath.util.PropertyGetterResolver;

public class SimpleStep implements Step {

	private String step;
	private Predicate predicate;
	
	public SimpleStep(String step) {
		this.step = step;
	}
	
	@Override
	public Object evaluate(Context context) {
		Object object = context.get(ContextKey.CURRENT_NODE);
		PropertyGetter<Object> resolve = PropertyGetterResolver.resolve(object);
		Object stepResult = resolve.get(step, object);
		if (predicate != null) {
			context.set(ContextKey.CURRENT_NODE, stepResult);
			return predicate.apply(context);
		}
		return stepResult;
	}

	@Override
	public void addPredicate(Predicate predicate) {
		this.predicate = predicate;
	}
}
